//
//  YCResetPasswordViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/3/1.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCResetPasswordViewController.h"

@interface YCResetPasswordViewController ()
{
    UITableView *m_tableView;
    
    UIView *_backView1;
    UIView *_backView2;
    UILabel *m_label1;
    UILabel *m_label2;
    UILabel *m_label3;
    
    UITextField *m_textfield1;
    UITextField *m_textfield2;
    UITextField *m_textfield3;
    
    UIButton *m_btnConfirm;
    YCResultView *m_hudView;
}
@end

@implementation YCResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"修改密码";
    self.navigationItem.leftBarButtonItem=[self tbarBackButton];
    
    [self.view setBackgroundColor:WHITE_COLOR];
    [self addMainView];
    m_hudView=[[YCResultView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Height)];
    
    [self textChange];
}
-(void)addMainView
{
    _backView1=[[UIView alloc]init];
    [_backView1 setBackgroundColor:WHITE_COLOR];
    [self.view addSubview:_backView1];
    [_backView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kDistance);
        make.right.equalTo(self.view).offset(-kDistance);
        make.top.equalTo(self.view).offset(kNAV_HEIGHT+kDistance);
        make.height.mas_equalTo(50);
    }];
    CGFloat backWidth=kMainScreen_Width-2*kDistance;
    [_backView1 addShadowWith:CGSizeMake(backWidth, 50)];
    
    m_label1=[[UILabel alloc]initWithFrame:CGRectMake(kDistance, 0, 65, 50)];
    [m_label1 setFont:[UIFont LightFontOfSize:16]];
    [m_label1 setTextColor:TEXT_DEEP];
    [m_label1 setText:@"当前密码"];
    [_backView1 addSubview:m_label1];
    
    m_textfield1=[[UITextField alloc]init];
    [m_textfield1 setFont:[UIFont LightFontOfSize:15]];
    [m_textfield1 setPlaceholder:@"请输入当前密码" Corlor:DISABLE_COLOR];
    [m_textfield1 setSecureTextEntry:YES];
    [m_textfield1 setTextColor:TEXT_MIDDLE];
    [_backView1 addSubview:m_textfield1];
    [m_textfield1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(m_label1.right).offset(kDistance);
        make.right.equalTo(_backView1).offset(-kDistance);
        make.top.equalTo(_backView1).offset(0);
        make.bottom.equalTo(_backView1).offset(0);
    }];
    
    
    _backView2=[[UIView alloc]init];
    [_backView2 setBackgroundColor:WHITE_COLOR];
    [self.view addSubview:_backView2];
    [_backView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kDistance);
        make.right.equalTo(self.view).offset(-kDistance);
        make.top.equalTo(_backView1.bottom).offset(kSmallDistance);
        make.height.mas_equalTo(100);
    }];
    [_backView2 addShadowWith:CGSizeMake(kMainScreen_Width-2*kDistance, 100)];
    [_backView2.layer addSublayer:getLine(kDistance, kMainScreen_Width-3*kDistance, 50, 50, LINE_COLOR)];
    
    m_label2=[[UILabel alloc]initWithFrame:CGRectMake(kDistance, 0, 65, 50)];
    [m_label2 setFont:[UIFont LightFontOfSize:16]];
    [m_label2 setTextColor:TEXT_DEEP];
    [m_label2 setText:@"新密码"];
    [_backView2 addSubview:m_label2];

    m_label3=[[UILabel alloc]initWithFrame:CGRectMake(kDistance, 50, 65, 50)];
    [m_label3 setFont:[UIFont LightFontOfSize:16]];
    [m_label3 setTextColor:TEXT_DEEP];
    [m_label3 setText:@"确认密码"];
    [_backView2 addSubview:m_label3];
    
    
    m_textfield2=[[UITextField alloc]init];
    [m_textfield2 setFont:[UIFont LightFontOfSize:15]];
    [m_textfield2 setPlaceholder:@"请输入6位以上密码" Corlor:DISABLE_COLOR];
    [m_textfield2 setSecureTextEntry:YES];
    [m_textfield2 setTextColor:TEXT_MIDDLE];
    [_backView2 addSubview:m_textfield2];
    [m_textfield2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(m_label2.right).offset(kDistance);
        make.right.equalTo(_backView2).offset(-kDistance);
        make.top.equalTo(_backView2).offset(0);
        make.height.mas_equalTo(50);
    }];
    
    
    m_textfield3=[[UITextField alloc]init];
    [m_textfield3 setFont:[UIFont LightFontOfSize:15]];
    [m_textfield3 setPlaceholder:@"请重复输入一遍" Corlor:DISABLE_COLOR];
    [m_textfield3 setSecureTextEntry:YES];
    [m_textfield3 setTextColor:TEXT_MIDDLE];
    [_backView2 addSubview:m_textfield3];
    [m_textfield3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(m_label3.right).offset(kDistance);
        make.right.equalTo(_backView2).offset(-kDistance);
        make.bottom.equalTo(_backView2).offset(0);
        make.height.mas_equalTo(50);
    }];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange)name:UITextFieldTextDidChangeNotification object:m_textfield1];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange)name:UITextFieldTextDidChangeNotification object:m_textfield2];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange)name:UITextFieldTextDidChangeNotification object:m_textfield3];
    
    m_btnConfirm=[[UIButton alloc]init];
    [m_btnConfirm.titleLabel setFont:[UIFont LightFontOfSize:18]];
    [m_btnConfirm setBackgroundColor:MAIN_COLOR];
    [m_btnConfirm setTitle:@"确认" forState:UIControlStateNormal];
    [m_btnConfirm addTarget:self action:@selector(btnConfirmClicked:) forControlEvents:UIControlEventTouchUpInside];
    [m_btnConfirm setBackgroundImage:[UIImage imageWithColor:MAIN_COLOR] forState:UIControlStateHighlighted];
    m_btnConfirm.layer.cornerRadius=4.0f;
    [self.view addSubview:m_btnConfirm];
    [m_btnConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kDistance);
        make.right.equalTo(self.view).offset(-kDistance);
        make.top.equalTo(_backView2.bottom).offset(50);
        make.height.mas_equalTo(50);
    }];
    m_btnConfirm.enabled=NO;
    
}
-(void)textChange
{
    m_btnConfirm.enabled= (m_textfield1.text.length>5&&m_textfield2.text.length>5&&m_textfield3.text.length>5);
    if (m_btnConfirm.enabled) {
        [m_btnConfirm setBackgroundColor:MAIN_COLOR];
    }else{
        [m_btnConfirm setBackgroundColor:LIGHT_COLOR];
    }
}
-(void)btnConfirmClicked:(UIButton *)button
{
    [m_textfield1 resignFirstResponder];
    [m_textfield2 resignFirstResponder];
    [m_textfield3 resignFirstResponder];
    
    UserUpdateRequest *request = [UserUpdateRequest new];
    request.password = m_textfield2.text;
    request.repassword = m_textfield3.text;
    request.old_password = m_textfield1.text;
    [apiClient doUserUpdate:request success:^(ApiResponse *data, NSString *url) {
        UserUpdateResponse *response=[[UserUpdateResponse alloc]initWithCGResponse:data];
        [App shared].user.access_token = response.access_token;
        [[App shared] save];
        [[App shared] restore];
        [m_hudView showSuccessWith:data.message];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self goBack];
        });
        
    }failure:^(ApiResponse *data, NSString *url){
    }];

}

@end





